﻿using EONFULLSIN2.Common;
using EONFULLSIN2.Models.Dto.Menu;
using EONFULLSIN2.Services.DB;
using EONFULLSIN2.Services.Message;
using EONFULLSIN2.ViewModelBase;
using Prism.Events;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;

namespace EONFULLSIN2
{
    public class MainWindowModel : NavigationViewModel
    {
        public MainWindowModel(
            IRegionManager regionManager, 
            IDialogService dialog, 
            IMessageService messageBox, 
            IEventAggregator eventAggregator,
            IDbOperate<IFreeSqlMainContext> mainDbOperate) 
            : base(regionManager, dialog, messageBox, eventAggregator, mainDbOperate)
        {

            

        }


        private int _SelectedIndex;
        public int SelectedIndex
        {
            get { return _SelectedIndex; }
            set { _SelectedIndex = value; RaisePropertyChanged(); }
        }

        private MenuItemDto _SelectedItem;
        public MenuItemDto SelectedItem
        {
            get { return _SelectedItem; }
            set
            {
                if (value == null || string.IsNullOrWhiteSpace(value.Code))
                    return;

                _regionManager.Regions[PrismRegionManager.MainViewRegionName].RequestNavigate(value.Code, arg =>
                {
                    //没有导航成功 
                    if (arg.Result == false)
                    {
                        if (arg.Context.Parameters.ContainsKey("NameSpace"))
                        {
                            var view = arg.Context.Parameters.GetValue<string>("NameSpace");
                            var menu = AppInfo.MenuList.FirstOrDefault(a => a.Code == view);
                            if (menu != null)
                            {
                                var index = AppInfo.MenuList.IndexOf(menu);
                                if (index > -1)
                                {
                                    new Task(() =>
                                    {
                                        SelectedIndex = index;

                                    }).Start();
                                }
                            }
                        }
                    }
                    else
                    {
                        _SelectedItem = value;
                    }
                    RaisePropertyChanged();
                });
            }
        }


        protected override void OnViewLoaded()
        {
            SelectedItem = AppInfo.MenuList[0];
            base.OnViewLoaded();    
        }

    }
}
